<?php
/**
 * Created by PhpStorm.
 * User: HLG
 * Date: 2019/10/16
 * Time: 14:01
 */
namespace app\api\model;
use app\api\controller\Send;
use app\common\model\UploadFile;
use think\Request;
use think\Db;
class Mine extends BaseModel
{
    protected $resultSetType = 'collection';
    protected $table='f_user';

    //上传图片
    public function  upload(){
        return upload();
    }

    /**
     * @param $uid
     * 设置里面显示
     */
    public function mine_info($uid){
        $list = db('user')->where(array('user_id'=>$uid))->find();
        $arr['head_img'] = $list['avatarUrl'];
        $arr['uname'] = $list['nickName'];
        $arr['phone']  = $list['mobile'];
        $wx = db('bind_wxzfb')->where(array('type'=>1,'uid'=>$uid))->find();
        $zfb = db('bind_wxzfb')->where(array('type'=>2,'uid'=>$uid))->find();
        $real = db('authentication')->where(array('is_pay'=>1,'uid'=>$uid));
        if($real){
            $arr['real'] = 1;
        }else{
            $arr['real'] = 0;
        }
        if($wx){
            $arr['wx'] = 1;
        }else{
            $arr['wx'] = 0;
        }
        if($zfb){
            $arr['zfb'] = 1;
        }else{
            $arr['zfb'] = 0;
        }
        $status = ['code'=>1,'msg'=>'查询成功','data'=>$arr];
        return $status;
    }
    /**
     * 修改用户名昵称
     */
    public function edit_name($uid){
        $data = input('uname');
        $res = db('user')->where(array('user_id'=>$uid))->setField('nickName',$data);
        if($res){
            $status = ['code'=>1,'msg'=>'修改成功','data'=>''];
        }else{
            $status = ['code'=>0,'msg'=>'修改失败','data'=>''];
        }
        return $status;
    }
    /**
     * 修改头像
     */
    public function update_head($uid){
        $img = input('image');

        $res = db('user')->where(array('user_id'=>$uid))->setField('avatarUrl',$img);
        if($res){
            $status = ['code'=>1,'msg'=>'修改成功','data'=>''];
        }else{
            $status = ['code'=>0,'msg'=>'修改失败','data'=>''];
        }
        return $status;
    }
    /**
     * 修改登录密码
     */
    public function edit_password($uid){
        $yzm = new Send();
        $mobile = input('phone');
        $code = input('code');
        $old_pwd = input('old_pwd');
        $new_pwd = input('new_pwd');
        $qr_pwd = input('qr_pwd');
        $yzm->checkCode($code,$mobile);
        $u = db('user')->where(array('user'=>$uid))->find();
        if(md5($old_pwd) != $u['password']){
            $status = ['code'=>0,'msg'=>'旧密码输入错误','data'=>''];
            return $status;
            exit;
        }
        if(md5($new_pwd) != md5($qr_pwd)){
            $status = ['code'=>0,'msg'=>'两次输入的密码不一致','data'=>''];
            return $status;
            exit;
        }
        $arr = [
            'password'=>md5($new_pwd)
        ];
        $res = db('user')->where(array('user_id'=>$uid))->update($arr);
        if($res){
            $status = ['code'=>1,'msg'=>'修改成功','data'=>''];
        }else{
            $status = ['code'=>0,'msg'=>'修改失败','data'=>''];
        }
        return $status;
    }
    /**
     * 修改支付密码
     */
    public function edit_paypwd($uid){
        $yzm = new Send();
        $mobile = input('phone');
        $code = input('code');
        $old_pwd = input('old_pwd');
        $new_pwd = input('new_pwd');
        $qr_pwd = input('qr_pwd');
        $yzm->checkCode($code,$mobile);
        $u = db('user')->where(array('user'=>$uid))->find();
        if(md5($old_pwd) != $u['pay_password']){
            $status = ['code'=>0,'msg'=>'旧密码输入错误','data'=>''];
            return $status;
            exit;
        }
        if(md5($new_pwd) != md5($qr_pwd)){
            $status = ['code'=>0,'msg'=>'两次输入的密码不一致','data'=>''];
            return $status;
            exit;
        }
        $arr = [
            'pay_password'=>md5($new_pwd)
        ];
        $res = db('user')->where(array('user_id'=>$uid))->update($arr);
        if($res){
            $status = ['code'=>1,'msg'=>'修改成功','data'=>''];
        }else{
            $status = ['code'=>0,'msg'=>'修改失败','data'=>''];
        }
        return $status;
    }
    /**
     * 绑定微信支付宝显示
     */
    public function bind_xian($uid){
        $type = input('type');
        if($type == 'wx'){
            $class = 1;
        }else{
            $class = 2;
        }
        $list = db('bind_wxzfb')->where(array('uid'=>$uid,'type'=>$class))->find();
        if($list){
            $status = ['code'=>1,'msg'=>'成功','data'=>$list];
        }else{
            $status = ['code'=>0,'msg'=>'查询为空','data'=>''];
        }
        return $status;
    }
    /**
     * 绑定微信
     */
    public function bind_wxzfb($uid){

        $uname = input('uname');
        $phone = input('account');
        $pwd = input('pwd');
        $image = input('image');
        $type = input('type');

        if($type == 'wx'){
            $class = 1;
        }else{
            $class = 2;
        }
        $arr = [
            'account'=>$phone,
            'pwd' =>md5($pwd),
            'image'=>$image,
            'uname'=>$uname,
            'create_time'=>time(),
            'type'=>$class,
            'uid'=>$uid
        ];
        $find = db('bind_wxzfb')->where(array('uid'=>$uid,'type'=>$class))->find();
        if($find){
            $res = db('bind_wxzfb')->where(array('uid'=>$uid,'type'=>$class))->update($arr);
        }else{
            $arrs = getSetting();
            // dump($arr['pay_number']);die;
            $user = db('user')->where(array('user_id'=>$uid))->setInc('power',$arrs['pay_number']);
            suanli($uid,$arrs['pay_number']);
            $res = db('bind_wxzfb')->where(array('uid'=>$uid,'type'=>$class))->insert($arr);
        }
        if($res){
            $status = ['code'=>1,'msg'=>'成功','data'=>''];
        }else{
            $status = ['code'=>0,'msg'=>'失败','data'=>''];
        }
        return $status;
    }
    /**
     * 我的团队
     */
    public function mine_group($uid){
        $user = db('user')->where(array('user_id'=>$uid))->find();
        $group = db('dealer_referee')
            ->alias('r')
            ->join('user u','r.user_id = u.user_id','left')
            ->join('balance b','b.user_id = r.user_id','left')
            ->where("FIND_IN_SET($uid,r.path)")
            ->field('r.user_id,u.nickName,u.mobile,u.power,b.balance')
            ->select();
        $user_num = '';

        $user_balance = '';
        foreach ($group as $key =>$v){
            $user_num += db('user')->where('user_id',$v['user_id'])->sum('power');
            $user_balance += db('balance')->where('user_id',$v['user_id'])->sum('balance');
        }
        $minesl = db('user')->where('user_id',$uid)->find();
        $mine_balance = db('balance')->where('user_id',$uid)->find();
        //  print_r($user_balance);die;
        $arr['uname'] = $user['nickName'];
        $arr['mobile'] = $user['mobile'];
        $arr['group'] = $group;
        $arr['group_power'] = $user_num+$minesl['power'];
        $arr['group_balance'] = $user_balance+$mine_balance['balance'];
        $status = ['code'=>0,'msg'=>'uname:用户名,mobile:手机号,group:团队成员,group_power:团队总算力,group_balance:团队总收益','data'=>$arr];
        return $status;
    }
}